package per.kongtouxu.controller;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import per.kongtouxu.Util.CommonMethod;
import per.kongtouxu.domain.pojo.Admin;
import per.kongtouxu.domain.pojo.AdminLog;
import per.kongtouxu.domain.pojo.FileType;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.service.FileTypeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/fileType")
public class FileTypeController {
    @Autowired
    private FileTypeService fileTypeService;
    //分页查询
    @RequestMapping("/pageFind")
    public @ResponseBody PageBean<FileType> pageFind(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        String tailName = req.getParameter("tailName");
        String startScore = req.getParameter("startScore");
        String endScore = req.getParameter("endScore");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        if(startScore.equals("")){
            startScore="0";
        }
        if(endScore.equals("")){
            endScore="0";
        }
        //2.调用service
        PageBean<FileType> pageBean = fileTypeService.pageFind(tailName, Integer.parseInt(startScore), Integer.parseInt(endScore), Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        System.out.println(pageBean);
        //3.返回JSON
        return pageBean;
    }
    //新增文件类型
    @RequestMapping("/addFileType")
    public @ResponseBody Integer addFileType(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        Map<String, String[]> map = req.getParameterMap();
        //2.封装
        FileType fileType = new FileType();
        BeanUtils.populate(fileType,map);
        System.out.println("fileType:"+fileType);
        //3.后台日志插入
        AdminLog adminLog = CommonMethod.getAdminLog(req);
        adminLog.setDoName("增加文件类型");
        //4.调用service
        Integer result = fileTypeService.addFileType(fileType, adminLog);
        //5.返回JSON
        return result;
    }
    //删除文件
    @RequestMapping("/deleteFileType")
    public @ResponseBody Integer deleteFileType(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        String ftId = req.getParameter("ftId");
        //3.后台日志插入
        AdminLog adminLog = CommonMethod.getAdminLog(req);
        adminLog.setDoName("删除文件类型");
        //2.调用service
        Integer result = fileTypeService.deleteFileType(Integer.parseInt(ftId), adminLog);
        //4.返回JSON
        return result;
    }
    //修改操作
    @RequestMapping("/updateFileType")
    public @ResponseBody Integer updateFileType(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        Map<String, String[]> map = req.getParameterMap();
        //2.封装
        FileType fileType = new FileType();
        BeanUtils.populate(fileType,map);
        System.out.println("fileType:"+fileType);
        //3.后台日志插入
        AdminLog adminLog = CommonMethod.getAdminLog(req);
        adminLog.setDoName("修改文件类型");
        //4.调用service
        Integer result = fileTypeService.updateFileType(fileType,adminLog);
        //5.返回JSON
        return result;

    }

}
